home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************
- * *
- * StringToC - convert a string to a C version of the string *
- * *
- * Copyright © 1990 Karl J. Smith. All Rights Reserved *
- * *
- * email: ksmith@jarthur.claremont.edu *
- * *
- * 8/14/90 *
- * *
- * When debugging, set the project type to application, *
- * and add the ANSI project. *
- * *
- * When all your bugs are out, 'undef' DEBUG, set the project *
- * type to Code Resource, with type = ACMD, name = <the name *
- * of the function>, file type to 'AEXT', and the purgeable *
- * flag set to true. *
- * *
- * Also, you need to remove the ANSI project from the ACMD *
- * project and replace it with the ANSI-A4 project if you use *
- * any functions in ANSI, such as strlen. *
- * *
- * If you are not using THINK C, I'm not quite sure what you *
- * should do. :-) *
- * *
- ****************************************************************/
-
- #undef DEBUG
-
- #include <stdio.h>
- #include <string.h>
-
- /* Prototypes */
- char *
- #ifdef DEBUG
- dummy(char *inStr);
- #else DEBUG
- main(char *inStr);
- #endif DEBUG
-
- char *
- #ifdef DEBUG
- dummy(inStr)
- #else DEBUG
- main(inStr)
- #endif DEBUG
- char *inStr;
- {
- char *currentChar; /* Current position in input string */
- char *resultString; /* String to return */
- char *destination; /* Current position in result string */
- OSErr memError; /* Error, if any */
-
- Size returnLength; /* Length of string to return */
-
- resultString = inStr; /* In case the length is 0 */
- returnLength = GetPtrSize(resultString);
-
- if (returnLength != 0)
- {
- resultString = NewPtr(returnLength);
- if (resultString != NULL)
- {
- destination = resultString;
- for (currentChar = inStr;*currentChar != 0;currentChar++)
- {
-
- if ( (*currentChar == '\n') || (*currentChar == '\t')
- || (*currentChar == '\v') || (*currentChar == '\b')
- || (*currentChar == '\r') || (*currentChar == '\f')
- || (*currentChar == '\a') || (*currentChar == '\a')
- || (*currentChar == '\\') || (*currentChar == '\'')
- || (*currentChar == '\"') )
-
- {
- SetPtrSize(resultString, ++returnLength);
-
- if (MemError() == 0)
- switch (*currentChar)
- {
- case '\n': *destination++ = '\\'; *destination++ = 'n';
- break;
- case '\t': *destination++ = '\\'; *destination++ = 't';
- break;
- case '\v': *destination++ = '\\'; *destination++ = 'v';
- break;
- case '\b': *destination++ = '\\'; *destination++ = 'b';
- break;
- case '\r': *destination++ = '\\'; *destination++ = 'r';
- break;
- case '\f': *destination++ = '\\'; *destination++ = 'f';
- break;
- case '\a': *destination++ = '\\'; *destination++ = 'a';
- break;
- case '\\': *destination++ = '\\'; *destination++ = '\\';
- break;
- case '\'': *destination++ = '\\'; *destination++ = '\'';
- break;
- case '\"': *destination++ = '\\'; *destination++ = '\"';
- break;
- default:
- break;
- }
- else
- {
- SysBeep(5);
- DisposPtr(resultString);
- return(inStr);
- }
- }
- else /* It's just an ordinary character */
- {
- *destination++ = *currentChar; /* Just copy the character */
- }
- }
- *destination = 0; /* Terminate the string */
- DisposPtr(inStr);
- }
- else /* We don't have enough memory */
- {
- SysBeep(5);
- return(inStr);
- }
- }
- return(resultString);
- }
-
-
- #ifdef DEBUG
-
- char * ConvertRtoN(char *theStr);
-
- char * ConvertRtoN(theStr)
- char *theStr;
- {
- char *currentChar;
-
- for (currentChar = theStr;*currentChar != 0; currentChar++)
- if (*currentChar == '\r')
- *currentChar = '\n';
- }
-
- main()
- {
- char *str = "Boing\rMonkey\r\n\t\v\b\f\a\\\'\"";
- char *ret;
- char *once;
- char *currentChar;
-
- once = NewPtr(256L);
-
- strcpy(once, str);
-
- ret = dummy(once);
-
- ConvertRtoN(ret); /* Convert '\r' to '\n' so we can see them in a printf */
- ConvertRtoN(str);
-
- printf("The original is:\n\n%s", str);
- printf("\n----\n\nThe result is:\n\n");
-
- for(currentChar = ret;*currentChar != 0;currentChar++)
- {
- printf("%c", *currentChar);
- }
-
- }
- #endif DEBUG
-
-
-
-
-
-
-